import java.util.Random;

/**
 * 线程分组使用
 */
public class ThreadDemo12 {
    public static void main(String[] args) {
        //1.创建一个线程分组(女子100米比赛)
        ThreadGroup group=new ThreadGroup("thread-group");
        //2.定义一个公共的任务(线程任务)
        Runnable runTask= new Runnable() {
            @Override
            public void run(){//业务
                //生成一个1-3秒的随机数
                int num=(1+new Random().nextInt(3));
                try {
                    //跑了n秒之后到达终点
                    Thread.sleep(num*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //得到执行此方法的线程
                Thread t=Thread.currentThread();
                System.out.println(t.getName()+"--选手到达终点:"+num+"s");
            }
        };
        //3.线程(运动员)
        Thread t1=new Thread(group,runTask);
        Thread t2=new Thread(group,runTask);
        Thread t3=new Thread(group,runTask);

        //起跑
        t1.start();
        t2.start();
        t3.start();

        //所有人全部到达终点之后宣布成绩
        while(group.activeGroupCount()  == 0){
        }
        System.out.println("宣布比赛成绩");
    }
}
